package com.betameta.rotativobh;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.betameta.rotativobh.base.LocationBase;
import com.betameta.rotativobh.enums.RotaryType;
import com.betameta.rotativobh.objects.RotaryMapMarker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MapTestActivity extends LocationBase {
	static final LatLng CASA = new LatLng(-19.921688,-43.948812);
	static final LatLng BETAMETA = new LatLng(-19.922051,-43.947024);
	private GoogleMap map;
	private Circle myPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_test);
		
		LocationManager locationManager = 
	            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	    
	    if (!gpsEnabled) {
	    	
	    }	    
	    
	    /*Marker kiel = map.addMarker(new MarkerOptions()
	        .position(BETAMETA)
	        .title("AP901")
	        .snippet("BetaMeta Test")
	        .icon(BitmapDescriptorFactory
	            .fromResource(R.drawable.ic_launcher)));*/

	    // Zoom in, animating the camera.
	    //map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    getMenuInflater().inflate(R.menu.map_test, menu);
	    return true;
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
		        .getMap();
		String teste = "";
		    
		List<RotaryMapMarker> markers = new ArrayList<RotaryMapMarker>();
	    try {
		    Resources resources = getResources();
		    R.array array = new R.array();
		    for (Field field : R.array.class.getFields()) {
		    	if (field.getName().contains("rotary_position")) {
		    		String[] values = resources.getStringArray(field.getInt(array));
		    		if (values != null) {
		    			RotaryMapMarker marker = new RotaryMapMarker();
		    			marker.setPosition(new LatLng(Double.parseDouble(values[0]), Double.parseDouble(values[1])));
		    			marker.setRotaryType(RotaryType.getByValue(Integer.parseInt((values[2]))));
		    			markers.add(marker);
		    		}
		    	}
		    }
	    } catch (Exception e) {
	    	teste = e.getMessage();
	    }
	    teste = "";
	    
	    for (RotaryMapMarker rotaryMapMarker : markers) {
	    	map.addMarker(new MarkerOptions()
	        .position(rotaryMapMarker.getPosition())
	        .title("Segunda a sexta-feira")
	        .snippet(rotaryMapMarker.getRotaryType().getDescription())
	        .icon(rotaryMapMarker.getIcon()));
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	public void onConnected(Bundle dataBundle) {
		super.onConnected(dataBundle);
		//Move the camera instantly to hamburg with a zoom of 17.
	    LatLng currentPosition = getCurrentPosition();

	    CircleOptions circleOptions = new CircleOptions()
	    								.center(BETAMETA)
	    								.radius(10)
	    								.strokeColor(Color.TRANSPARENT)
	    								.fillColor(Color.GREEN)
	    								.visible(false);
    	myPosition = map.addCircle(circleOptions);
    	
	    if (currentPosition != null) {
	    	map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));
	    	myPosition.setCenter(currentPosition);
        	myPosition.setVisible(true);
	    } else {
	    	map.moveCamera(CameraUpdateFactory.newLatLngZoom(BETAMETA, 17));
	    }
	}
	
	@Override
    public void onLocationChanged(Location location) {
        // Report to the UI that the location was updated
        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        
        LatLng currentPosition = getCurrentPosition();
        if (currentPosition != null) {
        	myPosition.setCenter(currentPosition);
        	myPosition.setVisible(true);
        	map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));
        } else {
        	myPosition.setVisible(false);
        }
    }
}
